package com.cdsz.boot.system.activity;

import com.cdsz.boot.biz.modules.system.entity.SysUser;
import com.cdsz.boot.biz.modules.system.mapper.SysUserMapper;
import com.cdsz.boot.system.activity.model.ActivitiSystemUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        ActivitiSystemUser activitiSystemUser = new ActivitiSystemUser();
        activitiSystemUser.setUsername(username);
        SysUser sysUser = sysUserMapper.getUserByName(username);
        if(sysUser == null) {
            return activitiSystemUser;
        }
        activitiSystemUser.setPassword(sysUser.getPassword());
        activitiSystemUser.setAccountNonExpired(true);
        activitiSystemUser.setCredentialsNonExpired(true);
        activitiSystemUser.setEnabled(Objects.equals(sysUser.getDelFlag(), 0));
        activitiSystemUser.setAccountNonLocked(Objects.equals(sysUser.getStatus(), 1));
        return activitiSystemUser;
    }
}
